<template>
	<view>
		<u--textarea 
			:disabled="disabled || !writable" 
			:placeholder="placeholder" 
			border="bottom" 
			:maxlength="-1"
			v-model="val"
			@input="textareaChange"
		></u--textarea>
		<view v-show="required && validator" class="requiredMessage">{{ requiredMessage }}</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			val: '',
			validator: false
		};
	},
	props: {
		value: {
			type: String,
			default: ''
		},
		placeholder: {
			type: String,
			default: '请输入内容'
		},
		required: {
			type: Boolean,
			default: false
		},
		requiredMessage: {
			type: String,
			default: '值不能为空'
		},
		disabled: {
			type: Boolean,
			default: false
		},
		writable: {
			type: Boolean,
			default: true
		}
	},
	computed: {
		validateStatus() {
			return this.$store?.state?.validate?.status || false
		}
	},
	watch: {
		value: {
			handler () {
				this.val = this.value || ''
			},
			immediate: true,
			deep: false
		},
		validateStatus() {
			if(this.validateStatus && this.required) {
				if(!this.val) {
					this.validator = true
				} else {
					this.validator = false
				}
			}
		},
		val() {
			if(this.validateStatus && this.required) {
				if(!this.val) {
					this.validator = true
				} else {
					this.validator = false
				}
			}
		}
	},
	methods: {
		// 输入框改变
		textareaChange(e) {
			this.$emit('input', this.val)
		}
	}
}
</script>

<style scoped>
.requiredMessage {
	width: 100%;
	height: 50rpx;
	line-height: 50rpx;
	color: #f56c6c;
	font-size: 22rpx;
}
</style>